package com.dataart.training.crimequalification.service;

import com.dataart.training.crimequalification.dao.QualificationTaskDAO;
import com.dataart.training.crimequalification.entity.QualificationTaskEntity;
import com.dataart.training.crimequalification.entity.TaskSolutionResultEntity;
import com.dataart.training.crimequalification.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class QualificationTaskServiceImpl implements QualificationTaskService {

    //-------------------------------------------------------------

    @Autowired
    private QualificationTaskDAO qualificationTaskDAO;

    //-------------------------------------------------------------

    /**
     * @return
     */
    @Override
    public List<QualificationTaskEntity> getAllTasks() {
        return qualificationTaskDAO.getAllTasks();
    }

    /**
     * @param taskId
     * @return
     */
    @Override
    public QualificationTaskEntity getTaskById(Integer taskId) {
        return qualificationTaskDAO.getTaskById(taskId);
    }

    /**
     * @param solutionResult
     */
    @Override
    public void saveUserSubmittedTaskSolutionResult(TaskSolutionResultEntity solutionResult) {
        qualificationTaskDAO.saveUserSubmittedTaskSolutionResult(solutionResult);
    }

    /**
     * @param user
     * @return
     */
    @Override
    public List<TaskSolutionResultEntity> getAllUserSolutions(UserEntity user) {

        List<TaskSolutionResultEntity> resultEntityList = new ArrayList<>();

        for (TaskSolutionResultEntity sol : qualificationTaskDAO.getAllSolutions()) {
            if (sol.getUser().getUserID().equals(user.getUserID())) {
                resultEntityList.add(sol);
            }
        }

        return resultEntityList;
    }

}
